package com.example.homeworkmanagement.controller;


import com.example.homeworkmanagement.pojo.Courses;
import com.example.homeworkmanagement.service.CoursesService;
import com.example.homeworkmanagement.vo.ResultVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 拒绝加班
 * @since 2025-05-25
 */
@RestController
@RequestMapping("/courses")
@Api(tags="课程管理",description="提供课程管理API接口")
@CrossOrigin
public class CoursesController {
    @Autowired
    private CoursesService coursesService;

    // 获取课程列表
    @PostMapping("/getCourseList")
    @ApiOperation("获取课程列表")
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "string", name = "courseId", value = "课程id", required = false),
            @ApiImplicitParam(dataType = "string", name = "courseName", value = "课程名称", required = false),
            @ApiImplicitParam(dataType = "string", name = "department", value = "课程所属院系", required = false),
            @ApiImplicitParam(dataType = "double", name = "courseCredits", value = "课程学分", required = false),
            @ApiImplicitParam(dataType = "integer", name = "page", value = "页码", required = true, defaultValue = "1"),
            @ApiImplicitParam(dataType = "integer", name = "size", value = "每页大小", required = true, defaultValue = "10")
    })
    public ResultVO<List<Courses>> getCourseList(
            String courseId,
            String courseName,
            String department,
            Double courseCredits,
            Integer page,
            Integer size) {
        // 处理 page 为 null 的情况，设置默认值
        if (page == null || page <= 0) {
            page = 1;
        }
        // 处理 size 为 null 的情况，设置默认值
        if (size == null || size <= 0) {
            size = 10;
        }
        Courses courses = new Courses();
        courses.setCourseId(courseId);
        courses.setCourseName(courseName);
        courses.setDepartment(department);
        courses.setCourseCredits(courseCredits);
        return coursesService.getCourseList(courses, page, size);
    }
    // 添加课程
    @PostMapping(value = "/addCourse")
    @ApiOperation("添加课程")
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "string", name = "courseId", value = "课程id", required = true),
            @ApiImplicitParam(dataType = "string", name = "courseName", value = "课程名称", required = true),
            @ApiImplicitParam(dataType = "string", name = "department", value = "课程所属院系", required = true),
            @ApiImplicitParam(dataType = "double", name = "courseCredits", value = "课程学分", required = true),
            @ApiImplicitParam(dataType = "string", name = "courseDescription", value = "课程描述", required = false)
    })
    public ResultVO<Courses> addCourse(
            String courseId,
            String courseName,
            String department,
            Double courseCredits,
            String courseDescription) {
        Courses course = new Courses();
        course.setCourseId(courseId);
        course.setCourseName(courseName);
        course.setDepartment(department);
        course.setCourseCredits(courseCredits);
        course.setCourseDescription(courseDescription);
        return coursesService.addCourse(course);
    }

    // 更新课程
    @PutMapping("/updateCourse")
    @ApiOperation("更新课程")
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "string", name = "courseId", value = "课程id", required = true),
            @ApiImplicitParam(dataType = "string", name = "courseName", value = "课程名称", required = false),
            @ApiImplicitParam(dataType = "string", name = "department", value = "课程所属院系", required = false),
            @ApiImplicitParam(dataType = "double", name = "courseCredits", value = "课程学分", required = false),
            @ApiImplicitParam(dataType = "string", name = "courseDescription", value = "课程描述", required = false)
    })
    public ResultVO<Courses> updateCourse(Courses course) {
        return coursesService.updateCourse(course);
    }

    // 删除课程
    @DeleteMapping("/deleteCourse")
    @ApiOperation("删除课程")
    @ApiImplicitParam(dataType = "string", name = "courseId", value = "课程id", required = true)
    public ResultVO<Courses> deleteCourse(String courseId) {
        return coursesService.deleteCourse(courseId);
    }

    // 获取课程详细信息
    @GetMapping("/getCourseDetail")
    @ApiOperation("获取课程详细信息")
    @ApiImplicitParam(dataType = "string", name = "courseId", value = "课程id", required = true)
    public ResultVO<Courses> getCourseDetail(String courseId) {
        return coursesService.getCourseDetail(courseId);
    }

    // 根据学院获取课程列表（不分页）
    @GetMapping("/getCourseListByDepartment")
    @ApiOperation("根据学院获取课程列表")
    @ApiImplicitParam(dataType = "string", name = "department", value = "学院名称", required = true)
    public ResultVO<List<Courses>> getCourseListByDepartment(String department) {
        return coursesService.getCourseListByDepartment(department);
    }

    // 根据课程名称获取课程id
    @GetMapping("/getCoureIdByName")
    @ApiOperation("根据课程名称获取课程id")
    @ApiImplicitParam(dataType = "string", name = "courseName", value = "课程名称", required = true)
    public ResultVO<String> getCourseIdByName(String courseName) {
        return coursesService.getCourseIdByName(courseName);
    }
}
